/*
 * simple testbed to run the chordlib
 * 
 * Choi Jonghwan <saxboy@saxboy.pe.kr>
 * 2005/02/19
 */

#include <stdio.h>
#include "note.h"
#include "chord.h"
#include "song.h"
#include "songrenderer.h"
#include "songexporter.h"

static int print_abc(CHORD c)
{
	int i=0; 

	if (c==NULL) return -1;

	printf("X:1\n"); 
	printf("T:SaxyChord Test\n"); 
	printf("R:tango\n"); 
	printf("M:4/4\n"); 
	printf("L:1/4\n"); 
	printf("K:C\n"); 
	//printf("P:A\n"); 

	/* start of bars */
	printf("P: A\n| "); 
	printf(" \"%s%d\" ", note_GetStaticAbcString(&(c->basenote)), c->chord_num ); 
	for (i=0; i< MAX_CHORD_NOTES; i++)
	{
		NOTE n = c->notes[i]; 
		if ( n==NULL) continue; 

		printf("%s", note_GetStaticAbcString(n)); 
		
	}
	//printf("| "); 
	printf("|]\n"); 

	return 1; 
}


int old_main(int argc, char **argv)
{
#if 0
	NOTE n=NULL; 

	n = note_NewFromString(argv[1]); 
	note_Print(n); 
	//note_RaiseHalf(n); 
	note_RaiseInterval(n, INTERVAL_MINOR, 3); 
	note_Print(n); 
#endif

#if 0
	CHORD c = NULL; 
	c = chord_NewFromString(argv[1]); 
	print_abc(c); 
	//chord_Print(c); 
	//
#endif

	SONG s=NULL; 
	SONGSTYLE songstyle=NULL; 

	s= song_New(); 
	if (s==NULL) return -1; 

	//song_SetMeasure(s, 6, 8); 
	song_SetMeasure(s, 4, 4); 

	/* construct new song */
	{
		BAR b=NULL; 

		/* FIXME: how to handle various chords? */
		//b = bar_NewFromString("Cmaj7 - Cm7 - "); 
		//
#if 1
		b = bar_NewFromString("CM7 - - - - -"); 
		song_AddBar(s, b); 

		b = bar_NewFromString("Dm7 - - - - -"); 
		song_AddBar(s, b); 

		b = bar_NewFromString("G7 - - - - -"); 
		song_AddBar(s, b); 

		b = bar_NewFromString("C7 - - - - -"); 
		song_AddBar(s, b); 

		/////// 
		b = bar_NewFromString("F7 - - - - -"); 
		song_AddBar(s, b); 

		b = bar_NewFromString("D7 - - - - -"); 
		song_AddBar(s, b); 

		b = bar_NewFromString("C#7 - - - - -"); 
		song_AddBar(s, b); 

		b = bar_NewFromString("C7 - - - - -"); 
		song_AddBar(s, b); 

		/////// 
		b = bar_NewFromString("G7 - - - - -"); 
		song_AddBar(s, b); 

		b = bar_NewFromString("F7 - - - - -"); 
		song_AddBar(s, b); 

		b = bar_NewFromString("C7 - - - - -"); 
		song_AddBar(s, b); 

		b = bar_NewFromString("C7 - - - - -"); 
		song_AddBar(s, b); 
#endif


	}
setup_songstyle:
	{
		songstyle = songstyle_New(); 
		if (songstyle==NULL) goto error; 

		songstyle_Load(songstyle, "jazz"); 
	}

	//song_Print(s, stdout); 
	
song_render:
	/* render it */
	{
		SONGRENDERER songrenderer=NULL; 

		songrenderer = songrenderer_New(); 
		if (songrenderer==NULL) goto error; 

		songrenderer_SetSong(songrenderer, s); 
		songrenderer_SetStyle(songrenderer, songstyle); 

		if ( songrenderer_Run(songrenderer) > 0 )
			goto song_export; 

		songrenderer_Delete(songrenderer); 
		return -1; 
	}

song_export:
	/* export it */
	{
		SONGEXPORTER songexporter=NULL; 
		songexporter = songexporter_NewWithType("ABC"); 
		if ( songexporter==NULL) goto error; 

		songexporter_SetSong(songexporter, s); 

		if ( songexporter_Run(songexporter) > 0 )
			goto song_done; 

		songexporter_Delete(songexporter); 
		return -1; 
	}

song_done:
	songstyle_Delete(songstyle); 
	song_Delete(s); 

	return 1; 

error:
	song_Delete(s);
	return -1; 
}

